package sanshui.system.trade.factory.trade.strategy.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import sanshui.system.trade.db.entity.TradeTargetCode;
import sanshui.system.trade.factory.data.Bar;
import sanshui.system.trade.factory.data.DataMarketType;
import sanshui.system.trade.factory.data.TradeFrequency;
import sanshui.system.trade.factory.data.TradeSystemType;
import sanshui.system.trade.factory.report.ReportTask;
import sanshui.system.trade.factory.trade.TradeSystemContext;
import sanshui.system.trade.factory.trade.indicator.IndicatorResult;
import sanshui.system.trade.factory.trade.strategy.AbstractStrategy;
import sanshui.system.trade.factory.trade.strategy.StrategyCallType;
import sanshui.system.trade.factory.trade.strategy.StrategyCode;
import sanshui.system.trade.factory.trade.strategy.StrategyResult;
import sanshui.system.trade.model.StockInfoModel;
import sanshui.system.trade.model.WenCaiModel;
import sanshui.system.trade.service.ITradeTargetCodeService;
import sanshui.system.trade.util.CommonTimeUtil;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 问财：Python接口，所以无法回测
 * "中证2000，非地产股，非银行股，非科创板，非创业板，非st股，
 * 总市值小于70亿，10日均线大于60日均线，30日线上移，最近3个月涨跌幅小于30%，总市值从小到大排列"
 * */
@Component
public class WenCaiZz2000Strategy extends AbstractStrategy {
    private static final Logger logger = LoggerFactory.getLogger(WenCaiZz2000Strategy.class);

    @Resource
    ReportTask reportTask;

    public String ZZ2000_QS = "中证2000，非地产股，非银行股，非科创板，非创业板，非st股，总市值小于70亿，10日均线大于60日均线，30日线上移，最近3个月涨跌幅小于30%，总市值从小到大排列";

    @Override
    public StrategyCode getStrategyCode() {
        return StrategyCode.ZZ2000;
    }

    /**
     * 不用操作
     */
    @Override
    public StrategyResult exec(String code, IndicatorResult indicatorResult, TradeSystemContext tradeSystemContext) {
        StrategyResult strategyResult = new StrategyResult();
        strategyResult.setCode(code);
        strategyResult.setStrategyCallType(StrategyCallType.NOTHING);
        strategyResult.setNumAround(1L);
        strategyResult.setPriceAround(100L);

        // 回测需要注意频率
        if (tradeSystemContext.getTradeSystemType().compareTo(TradeSystemType.BACK_TRADE) == 0){
            // 回测系统不做处理
            return strategyResult;
        }

        LocalDateTime current = tradeSystemContext.getTimeManager().timestampCurrent;

        if (!CommonTimeUtil.getNowTime8(current).equalsIgnoreCase(CommonTimeUtil.getNowTime8())
        && tradeSystemContext.getTimeManager().frequency.equalsIgnoreCase(TradeFrequency.frequency_1d.getValue())){
            // 1d, 不是当天数据，可能实时系统在加载历史数据
            return strategyResult;
        }

        // 每个周期问财一下
        WenCaiModel wenCaiModel = reportTask.wencaiByPython(CommonTimeUtil.getNowTime8_CH(current) + "，" + ZZ2000_QS);
        // 最好是给与最新价格
        if (null == wenCaiModel){
            return strategyResult;
        }
        List<StockInfoModel> codeInfoList =
                wenCaiModel.getCodeInfoList();
        if (null != codeInfoList && !codeInfoList.isEmpty()){
            StockInfoModel stockInfoModel = codeInfoList.get(0);
            strategyResult.setCode(stockInfoModel.getCodeWencai());
            strategyResult.setName(stockInfoModel.getCodeNameWencai());
            strategyResult.setPrice(Float.valueOf(Float.parseFloat(stockInfoModel.getPriceWencai()) * 100).longValue());
            strategyResult.setStrategyCallType(StrategyCallType.BUY);
        }
        return strategyResult;
    }
}
